import com.Module.antiFake.*;
import com.Module.check.*;
import com.Module.codeBind.BatchCodeGrid;
import com.Module.codeBind.CodeBindGrid;
import com.Module.device.*;
import com.Module.flowLine.SktExceptionGrid;
import com.Module.organization.OrganizationGrid;
import com.Module.prms.*;
import com.Module.product.*;
import com.Module.recall.*;
import com.Module.systemConfig.*;
import com.Module.traceSource.TsCodeForRecordGrid;
import com.Module.traceSource.TsEventItemRecordGrid;
import com.Module.product.ProductAuditGrid;
import flash.display.DisplayObject;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

import flexlib.containers.SuperTabNavigator;

import mx.collections.ArrayCollection;
import mx.containers.VBox;
import mx.controls.Alert;
import mx.controls.LinkButton;
import mx.events.ItemClickEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

var userinfo:Object= new Object();
var codeInfo:String = "";
var hasWarnInfo:Boolean = false;
public static var orgsOptList:ArrayCollection = new ArrayCollection();

public function addTab(menuId:String,lbl:String, navigator:SuperTabNavigator,cclassName:String,condition:Object):void {
	if(lbl=="") lbl = "(Untitled)";
	var curNum:Number = nav.numChildren + 1;

	

	var child:VBox = new VBox();
	child.setStyle("closable", true);
	child.label = lbl;
	//child.icon = document_icon;
	//add 反射
	OrganizationGrid;roleGrid;userGrid;menuGrid;grantprms;ProductGrid;entGrantprms;
	TsEventItemRecordGrid;
	TsCodeForRecordGrid;
	BatchCodeGrid;
	CodeBindGrid;
	AntiFakeWarnGrid;
	AntiFakeScanHistoryGrid;
	AntiFakeProductRecallGrid;
	SktExceptionGrid;
	ProductQueryGrid;
	RecallRegGrid;
	AntiFakeComplainGrid;
	CheckTaskGrid;
	PersonalTaskGrid;
	ProductAuditGrid;
	//赋码物管理
	CategoryGrid;
	AttrGroupGrid;
	AttrItemGrid;

	DeviceBreakdownGrid;
	DeviceLifecycleGrid;

	DeviceControlGrid;
	//系统配置
	SystemConfigGrid;
	if(menuId!=""&&cclassName!=null){
		var classReference:Class = flash.utils.getDefinitionByName(cclassName) as Class;
		var menuObj:Object = new classReference();
		menuObj.setMenuId(menuId);
		try
		{
			menuObj.setIndex(this);
			menuObj.setParam(condition);
		} 
		catch(error:Error) 
		{
			
		}
		child.addChild(DisplayObject(menuObj));
	}
	

	/*
	if(lbl=="预警管理")
	{
		var antiFakeWarnGrid : AntiFakeWarnGrid =new AntiFakeWarnGrid();
		antiFakeWarnGrid.setParent(this);  
		child.addChild(antiFakeWarnGrid); 
	}
    */
	navigator.addChild(child);
	navigator.selectedIndex = nav.numChildren - 1
	
	var tabBtn:mx.controls.Button=nav.getTabAt(curNum-1) as mx.controls.Button; 
	tabBtn.addEventListener(MouseEvent.DOUBLE_CLICK,doubleCloseHandler);
}
private function labelselect(label:String,navigator:SuperTabNavigator):int
{
	for(var i:int=0;i<navigator.getChildren().length;i++)
	{
		var child:VBox =navigator.getChildAt(i) as VBox;
		
		if(child.label==label)
		{
			navigator.selectedIndex=i;
			return 1;
		}
	}
	return 0;
}



/**
 * 用户按下回车触发此方法
 */
private function keyPressListen(event:KeyboardEvent):void{
	if(event.keyCode==13){
		login_F();
	}
	this.keyDownHandler(event);
}



function login_F():void
{
	if(username.text==""||password.text=="")
	{
		Alert.show("请输入账号，密码！","提示");
		loginbtn.mouseEnabled=true;
		return ;
	}
	else
	{
		remoteObject.getUserInfo(username.text,password.text);
//		orgsAction.getJustList();
		loginbtn.mouseEnabled=false;
	}
}
public	function orgListHandler(event:ResultEvent):void
{
	orgsac = event.result as ArrayCollection;
	if(orgsac==null)
	{
		Alert.show("获取信息失败","提示");
		loginbtn.mouseEnabled=true;
		return;
	}
	
	for(var i:int=0;i<orgsac.length;i++)
	{
		var org:Object=orgsac.getItemAt(i);
		orgsmap.put(org.id,org);
	}
	
//	remoteObject.getUserInfo(username.text,password.text);
}
public	function getUserInfoHd(event:ResultEvent):void
{

	userinfo = event.result;
	if(userinfo!=null&&userinfo.cuserId!=null)
	{
		this.currentState="loginone";
	    orglist.dataProvider=userinfo.orgs;
		orglist.selectedIndex=0;
		orglist_changeHandler();
		userid = userinfo.cuserId;
		usernamestr = userinfo.cuserName;
		password = userinfo.cuserPwd;
		
			
	}
	else
	{
		Alert.show("账号或密码错误或者账号被禁用！","提示");
		this.currentState="login";
		loginbtn.mouseEnabled=true;
		return;
	}
	
	
	//orglist.labelFunction="orgname";
}




public	function getUserMenuHd(event:ResultEvent):void
{
	treeData1=event.result as ArrayCollection;
	//Alert.show(treeData1.length.toString());
	//Alert.show(treeData1.toString());
	myTree.dataProvider = treeData1;
	//myTree.showRoot=false;
	myTree.labelField="cmenuName";
	orgsAction.getChildrenNodeId(orgid);
//	for(var i:int =0 ;i<treeData1.length;i++)
//	{	var menuobj:menu = treeData1.getItemAt(i) as menu;
//		//roleoptrmobjt.getOperationByRoleIdAndMenuId(menuobj.cmenuId);
//	}
	
	
}

//回调函数,控制按钮是否显示
public function getOperationByRoleIdHandler(event:ResultEvent):void{
	try{
		orgsOptList =event.result as ArrayCollection;
		//Alert.show("orgsOptList.size()="+orgsOptList.length);
	}catch(error:Error){
		//Alert.show(error.message);
		trace("查询数据异常！");
	}
}


public function orgidsHandler(event:ResultEvent):void
{
	
	var orgidst:String =event.result as String;
	if(orgidst==""||orgidst==null || orgidst.length==0)
	{
		orgids="-1";
	}else
	{
		orgids=orgidst;
	}
	
	orgsAction.getSelfTree(orgid);
	
	
}
//获取企业绑定的应用
private function getorgApp(event:ResultEvent):void{
	orgsTreeAc = event.result as ArrayCollection;
	orgsAction.getOrgApp(orgid);
}
private function orgtreeHandler(event:ResultEvent):void
{	
	orgApplist = event.result as ArrayCollection;
	//orgsTreeAc = event.result as ArrayCollection;
	//Alert.show(treeData1.length.toString());
	loginSession.setSession(userid,usernamestr, roleid, orgid, orgids, orgsTreeAc, orgsac, rolename, orgnamestr, treeData1,orgApplist,password.text);
	//roleoptrmobj.getOptByRoleIdAndMenuId(treeData1);
}

public function orgname(item:Object):String
{
	var orgobj:Object = orgsmap.getItem(item.corgId);
	var orgname:String = orgobj.organizationName;
	return orgname;
}

public function linkBarSel(evt:ItemClickEvent):void
{
	if(evt.label=="退出")
	{
		loginSession.removeSession();
	}else if(evt.label.indexOf("预警")>-1){
		if(labelselect("预警管理",nav)==0){
			addTab("","预警管理",nav,"","");
		}
	}
	if(evt.label.indexOf("修改密码")>-1){
		showPwdChange();
	}
	/*if(evt.index==2){
		showLoginUser();
	}*/
}

public function showLoginUser():void{
	var loginer:loginUserMsg = new loginUserMsg;
	PopUpManager.addPopUp(loginer,this,true);
	PopUpManager.centerPopUp(loginer);
}
public function showPwdChange():void{
	var pwdChange:PwdChangeWin = new PwdChangeWin;
	PopUpManager.addPopUp(pwdChange,this,true);
	PopUpManager.centerPopUp(pwdChange);
	
}
public function removeSessionHandler(event:ResultEvent):void
{
	var obj:Object = event.result;
	if(obj.state){
		//orglist.selectedIndex=-1;
		//rolelist.selectedIndex=-1;
		for each(var tab:* in this.nav.getChildren()){   
			if(tab != this.nav.getChildAt(0))
				this.nav.removeChild(tab);   
		}  
		password.text = "";
		this.currentState="login";
		loginviwe.visible = true;
		loginbtn.mouseEnabled=true;
		
	}
	//Alert.show(obj.msg);
}

public function doubleCloseHandler(childIndex:int)
{
	this.nav.removeChildAt(this.nav.selectedIndex);
}


public function setSessionHandler(event:ResultEvent)
{
	//首次登陆，弹出提示框。
	var flag:String = userinfo.cfirstLogin;
	if(flag=="0"){
		firstLoginPag();
	}
	remoteObject.hasWarnInfoRight();
	
}

public function getSessionHd(event:ResultEvent)
{
	var obj:Object = event.result;
	orgid=obj.orgid;
	roleid = obj.roleid;
	orgids = obj.orgids;
	orgnamestr =obj.orgname;
	rolename = obj.rolename;
	orgsTreeAc = obj.orgtree;
	orgsac =obj.orgsac;
	usernamestr =obj.username;
	userid = obj.userid;
	treeData1 = obj.menutree as ArrayCollection;
	//Alert.show(treeData1.toString());
	myTree.dataProvider = treeData1;
	myTree.labelField="cmenuName";
	
}
public function getSession1Hd(event:ResultEvent)
{
	var obj:Object = event.result;
	orgid=obj.orgid;
	roleid = obj.roleid;
	orgnamestr =obj.orgname;
	rolename = obj.rolename;
	usernamestr =obj.username;
	userid = obj.userid;
	//Alert.show(userid+" orgid="+orgid);
	if(userid!=""&& userid!=null && orgid!="" && orgid!=null)
	{
		this.currentState="loginin";		
		loginSession.getSession();
		remoteObject.hasWarnInfoRight();
	}
	else
	{
		this.currentState="login";
		loginviwe.visible=true;
	}
}
public function firstLoginPag():void{
	var first:firstLogin = new firstLogin;
	PopUpManager.addPopUp(first,this,true);
	PopUpManager.centerPopUp(first);
}

private function faultHandler(event:FaultEvent):void{
	if(this.currentState=="login")
	{
		loginbtn.mouseEnabled=true;
	}
	Alert.show("出错：网络连接超时！");
}


private function hasWarnInfoHd(event:ResultEvent):void{
	hasWarnInfo = event.result as Boolean;
	if(!hasWarnInfo){
		var  warnBut:LinkButton = getLinkButtonWarnMessage();
		warnBut.parent.removeChild(warnBut);
	}else{
		var warnTimerOne:Timer = new Timer(50,1);
		warnTimerOne.addEventListener(TimerEvent.TIMER, warnMessageTimer);
		warnTimerOne.start();
		//预警定时器;
		if(warnTimer==null){
			warnTimer = new Timer(1000*60);
			warnTimer.addEventListener(TimerEvent.TIMER, warnMessageTimer);
			warnTimer.start();
		}
	}
}


public function closeTab(label:String,navigator:SuperTabNavigator):int
{
	
	for(var i:int=0;i<navigator.getChildren().length;i++)
	{
		var child:VBox =navigator.getChildAt(i) as VBox;
		
		if(child.label==label)
		{
			//navigator.selectedIndex=i;
			navigator.removeChildAt(i);
			return 1;
		}
	}
	return 0;
}


